package demo.socket.client;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class SocketClient {
    
    public static void main(String[] args) {
        
        int port = 9527;
        String ip = "127.0.0.1";
        int timeout = 10000;
        Socket socket = null;
        OutputStream os = null;
        
        try {
            System.out.println("[SocketClient.main] 连接服务端");
            socket = new Socket(ip, port);
            socket.setSoTimeout(timeout);
            
            String requestStr = "Hello World.";
            byte[] reqBytes = requestStr.getBytes();
            
            os = socket.getOutputStream();
            os.write(reqBytes);
            
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                try {
                    os.close();
                    System.out.println("Client os close");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                    System.out.println("Client socket close");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
}
